<?php
/* SVN FILE: $Id: lil_blogs_notify_plugin_controller.php 184 2009-10-21 18:52:07Z miha@nahtigal.com $ */
/**
 * Short description for lil_blogs_notify_controller.php
 *
 * Long description for lil_blogs_notify_controller.php
 *
 * PHP versions 4 and 5
 *
 * Copyright (c) 2009, Miha Nahtigal
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright (c) 2009, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil_blogs_notify
 * @subpackage    lil_blogs_notify
 * @since         v 1.0
 * @version       $Revision: 184 $
 * @modifiedby    $LastChangedBy: miha@nahtigal.com $
 * @lastmodified  $Date: 2009-10-21 18:52:07 +0000 (Wed, 21 Oct 2009) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
App::import('Controller', 'LilBlogs.LilBlogsPlugin');
/**
 * LilBlogsNotifyPlugin class
 *
 * @uses          LilBlogsNotifyPluginController
 * @package       lil_blogs_notify
 * @subpackage    lil_blogs_notify
 */
class LilBlogsNotifyPluginController extends LilBlogsPluginController {
/**
 * name property
 *
 * @var string 'LilBlogsNotifyPlugin'
 * @access public
 */
	var $name = 'LilBlogsNotifyPlugin';
/**
 * components property
 *
 * @var array
 * @access public
 */
	var $components = array('Email');
/**
 * initialize method
 *
 * This is a plugins initialization method
 *
 * @access public
 * @return void
 */
	function _initialize() {
		require_once dirname(__FILE__).DS.'..'.DS.'config'.DS.'core.php';
		
		$this->attachHandler('after_save_comment', '_afterSaveComment');
	}
/**
 * afterSaveComment method
 *
 * This function display message and sends email
 *
 * @param mixed $model
 * @param mixed $args
 * @access public
 * @return void
 */
	function _afterSaveComment($controller, $args) {
		$comment = $args['data'];
		
		if (in_array($comment['Comment']['status'], array(LILCOMMENT_APPROVED)) &&
			($email_to = Configure::read('LilBlogsNotify.commentedEmail')))
		{
			// get post and blog data
			$post = $controller->Post->find('first', array(
				'conditions' => array(
					'Post.id' => $comment['Comment']['post_id']
				),
				'contain' => array(
					Configure::read('LilBlogs.noBlogs') ? null : 'Blog'
				)
			));
			if (!$blog = Configure::read('LilBlogs.noBlogs')) {
				$blog = array('Blog' => $post['Blog']);
			}
			
			$this->set('blog', $blog);
			$this->set('post', $post);
			
			// cleanup comment
			$comment['Comment']['body'] = $controller->kses5Clean($comment['Comment']['body']);
			$this->set('comment', $comment);
			
			$this->Email->to = $email_to;
			$this->Email->from = 'lil_blogs@'.env('HTTP_HOST');
			$this->Email->subject = sprintf(
				__d('lil_blogs_notify', '[%1$s] New comment on "%2$s"', true),
				$blog['Blog']['name'],
				$post['Post']['title']
			);
			$this->Email->template = 'comment_notification';
			$this->Email->sendAs = 'text';
			$this->Email->delivery = Configure::read('LilBlogsNotify.emailDelivery');
			$this->Email->smtpOptions = Configure::read('LilBlogsNotify.emailSmtpOptions');
			$this->Email->send();
			
		}
		// redirect to self
	    $controller->redirect(Router::url(null, true));
	}
}
?>